package TwoClientGameConnection;
import java.util.Vector;

/**
 * This class is created upon needing to send a message
 * to a client or receiving a message from a stream.
 * It facilitates the creation of an XML string message.
 * @author Jace Ferguson
 * @filename TCGMessage.java
 */
public class TCGMessage {
	public static final int MESSAGE_TYPE_LOW = 0;
	public static final int MESSAGE_TYPE_NORMAL = 1;
	public static final int MESSAGE_TYPE_HIGH = 2;
	public static final int MESSAGE_TYPE_ERROR = 3;
	/**
	 * Convert a message object to a string to be sent to the clients.
	 * @param message	TCGMessage to be sent to the clients
	 * @return	The message as a string.
	 */
	public static String convertMessageToString(TCGMessage message)
	{
		TCGMessageCreator textMessage = new TCGMessageCreator(message);
		return textMessage.getStringMessage();
	}
	private Integer messageId;
	private String command;
	private Vector<parameter> parameters;

	private Integer type;

	/**
	 * Default constructor that takes an integer message id number.
	 * @param id	Integer message Id number
	 */
	public TCGMessage(Integer id)
	{
		this.setMessageId(id);
		this.messageSetup();
	}

	/**
	 * Default constructor that takes a string id number.
	 * @param id	String message id number.
	 */
	public TCGMessage(String id)
	{
		this.setMessageId(id);
		this.messageSetup();
	}

	/**
	 * Add a parameter to the message vector. A null paramname 
	 * will result in a default paramname of "parameterN" where N
	 * is the number of parameter. A null paramvalue will result in a
	 * empty string value.
	 * @param ParamName	String
	 * @param ParamValue	String
	 */
	public void addParameter(String ParamName, String ParamValue)
	{
		String name = "";
		String value = "";
		if(ParamName != null)
			name = ParamName;
		else
			name = "parameter" + this.parameters.size();

		if(ParamValue != null)
			value = ParamValue;
		else
			value = "";
		this.parameters.add(new parameter(name, value));
	}

	/**
	 * Retrieve the message command.
	 * @return	String
	 */
	public String getCommand()
	{
		return this.command;
	}

	/**
	 * Get the message id
	 * @return	Integer message id
	 */
	public Integer getMessageId()
	{
		return this.messageId;
	}

	/**
	 * Retrieve the message's importance type.
	 * @return	Integer message type
	 */
	public int getMessageType()
	{
		return this.type;
	}

	/**
	 * Get all of the parameters in a vector object.
	 * @return	Vector	message parameters
	 */
	public Vector<parameter> getParameters()
	{
		return this.parameters;
	}

	/**
	 * Retrieve a parameter value using its name (key).
	 * Returns an empty string if the parameter name could not be found.
	 * @param ParamName	String key 
	 * @return	mixed 	parameter value
	 */
	public String getParameterValue(String ParamName)
	{
		for(parameter param : this.parameters)
		{
			if(param.getParamName().equals(ParamName))
			{
				if(param.getParamValue() != null)
					return param.getParamValue();
				else
					return "";
			}
		}
		return "";
	}

	/**
	 * Initializes the object vector and sets the default message priority.
	 */
	public void messageSetup()
	{
		this.parameters = new Vector<parameter>();
		this.setMessageType(TCGMessage.MESSAGE_TYPE_NORMAL);
	}	
	/**
	 * Set the command string for the message. If it is null, the message
	 * will have an empty string command.
	 * @param command	String command to be performed
	 */
	public void setCommand(String command)
	{
		if(command != null)
			this.command = command;
		else
			this.command = "";
	}
	
	/**
	 * Sets the message id.
	 * @param id	Integer message id.
	 */
	public void setMessageId(Integer id)
	{
		this.messageId = id;
	}
	
	/**
	 * Sets the message id.
	 * @param id	String message id.
	 */
	public void setMessageId(String id)
	{
		this.messageId = Integer.valueOf(id);
	}
	
	/**
	 * Reset the default message type. If the user
	 * does pass in a valid type, it is reset to default.
	 * @param setType	Integer type value
	 */
	public void setMessageType(Integer setType)
	{
		if(setType == null || setType > 3 || setType < 0)
		{
			this.type = TCGMessage.MESSAGE_TYPE_NORMAL;
		}
		else
		{
			this.type = setType;
		}
	}
	
	/**
	 * Set the parameters using an outside vector.
	 * @param parameters	Vector
	 */
	public void setParameters(Vector<parameter> parameters)
	{
		this.parameters = parameters;
	}
}


